node {
    gid = sh (script: "stat -c %g /run/containerd/containerd.sock", returnStdout: true,).trim()
    docker.withRegistry('ccr.ccs.tencentyun.com', 'mind_release') {

        stage('Preparation') {
            checkout scm
        }

        stage('Build') {
            buildImage("ccr.ccs.tencentyun.com/mind_release/golang-build:v${env.BUILD_NUMBER}",
                    "dockerfiles/golang/dockerfile.build", "./dockerfiles/golang/")
            buildImage("ccr.ccs.tencentyun.com/mind_release/golang-runtime:v${env.BUILD_NUMBER}",
                    "dockerfiles/golang/dockerfile.runtime", "./dockerfiles/golang/")
        }
    }
}

def buildImage(imageName, dockerfile, buildDir) {
    try {
        image = docker.build(imageName, "--force-rm -f ${dockerfile} ${buildDir}")
        image.push()
    } catch (err) {
        echo err.getMessage()
    }
    sh "docker rmi ${imageName}"
}